//
//  HYOUHeadScrollView.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUHeadScrollView.h"
#import "HYOUWebViewController.h"
@interface HYOUHeadScrollView ()<UIScrollViewDelegate>

@property(nonatomic, retain) HYOUBaseScrollView *scroll;

@property(nonatomic, assign) NSInteger flag; //记录当前页码
@property(nonatomic, retain) UIPageControl *pageControl;

@property(nonatomic, retain) NSTimer *myTimer;

@property(nonatomic, assign) NSInteger index;
@property(nonatomic, assign) NSInteger number;

@property(nonatomic, retain) UIImageView *myImage;

@end

@implementation HYOUHeadScrollView
//-(void)dealloc{
//    [_allArray release];
//    [_delegate release];
//    [_scroll release];
//    [_pageControl release];
//    [_myImage release];
//    [_myTimer release];
//    [super dealloc];
//}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    
#pragma mark -- 审核 轮播图
    self.scroll = [[HYOUBaseScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
//    self.scroll.backgroundColor = [UIColor whiteColor];
    self.scroll.contentSize = CGSizeMake(self.frame.size.width * [self.allArray count], self.frame.size.height);
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = YES;
    self.scroll.delegate = self;
    [self addSubview:self.scroll];
//    [_scroll release];
    
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
    //1.记录当前页码,默认为0
    self.index = 1;
}
-(void)setAllArray:(NSMutableArray *)allArray{
    if (_allArray != allArray) {
        [_allArray release];
        _allArray = [allArray retain];
    }
//    NSLog(@"%ld",self.allArray.count);
    

    
    for (int i = 0; i < self.allArray.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH * i) + WIDTH, 0, WIDTH, 200)];
        HYOUHeadScroll *model = [self.allArray objectAtIndex:i];
        NSString *str = model.image_url;
        NSURL *url = [NSURL URLWithString:str];
        [image sd_setImageWithURL:url];
        image.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress:)];
        [image addGestureRecognizer:singleTap];
        [self.scroll addSubview:image];
        [image release];
    }
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    HYOUHeadScroll *model = [self.allArray objectAtIndex:self.allArray.count - 1];
    NSURL *url1 = [NSURL URLWithString:model.image_url];
    [imageView1 sd_setImageWithURL:url1];
    [self.scroll addSubview:imageView1];
    [imageView1 release];
//        NSLog(@"%ld(unsigned long)",self.allArray.count);
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * (self.allArray.count + 1), 0, WIDTH, 200)];
    HYOUHeadScroll *model2 = [self.allArray objectAtIndex:0];
    NSURL *url2 = [NSURL URLWithString:model2.image_url];
    [imageView2 sd_setImageWithURL:url2];
    [self.scroll addSubview:imageView2];
    [imageView2 release];
//        NSLog(@"%ld",self.allArray.count);
    
    id imageFirst = [self.allArray firstObject];
    id imageLast = [self.allArray lastObject];
    [self.allArray insertObject:imageLast atIndex:0];
    [self.allArray addObject:imageFirst];
//        NSLog(@"%ld",self.allArray.count);
    
    self.scroll.contentSize = CGSizeMake(WIDTH * (self.allArray.count + 2), 200);
    self.scroll.contentOffset = CGPointMake(0, 0);
    [self.scroll scrollRectToVisible:CGRectMake(WIDTH, 0, WIDTH, 200) animated:NO];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(30, self.frame.size.height + 120, (WIDTH + 100)/2, 30)];
    [self reloadInputViews];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((WIDTH - 260) / 2, self.scroll.frame.size.height - 40, 260, 30)];
    self.pageControl.numberOfPages = self.allArray.count - 2;
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self addSubview:self.pageControl];
    [_pageControl release];


    
}
-(void)buttonpress:(UIButton *)btn{
    self.number = self.pageControl.currentPage + 1;
    HYOUHeadScroll *model = self.allArray[self.number];
    if ([model.type isEqualToString:@"url"]) {
        [self.delegate showHeadWeb:model.target_url];
    }
    if ([model.type isEqualToString:@"collection"]) {
        NSLog(@"%@",model.target_id);
        NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=2&limit=20&offset=0",model.target_id];
        [self.delegate showHeadDetail:url];
    }
}
- (void)changePage:(UIPageControl *)page{
    
    [self.scroll setContentOffset:CGPointMake((page.currentPage + 1) * self.scroll.frame.size.width, 0) animated:YES];
}
-(void)timerAction:(NSTimer *)timer
{
    
    //使用scrollview自带的滚动动画,实现起来效果不好,有bug.所以需要自己写动画效果
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.scroll setContentOffset:CGPointMake((self.index + 1) * self.scroll.frame.size.width, 0)];
        
        
    }];
//    [self.scroll setContentOffset:CGPointMake((self.index + 1) * self.scroll.frame.size.width, 0) animated:YES];
    
    self.index++;
    
    //如果滑动到最后一页
    if (self.scroll.contentOffset.x == (self.allArray.count - 1) * self.scroll.frame.size.width) {
        //快速移动到第一页
        [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0)];
        
        self.index = 1;
    }
    
}

//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.scroll.frame.size.width) / scrollView.frame.size.width;
    
}

////当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.myTimer invalidate];
    self.myTimer = nil;
}
//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scroll == scrollView) {
        //每次减速结束都让scollView所有的scrollView子视图缩放变为1
        for (UIScrollView * scrollv in scrollView.subviews) {
            if ([scrollv isKindOfClass:[UIScrollView class]]) {
                scrollv.zoomScale = 1.0;
            }
        }
        
        
        if (self.scroll.contentOffset.x >= self.scroll.frame.size.width * (self.allArray.count - 1)) {
            
            NSLog(@"最后一页");//循环滚动
            [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width, 0) animated:NO];
        }
        if (self.scroll.contentOffset.x <= 0) {
            
            [self.scroll setContentOffset:CGPointMake(self.scroll.frame.size.width * (self.allArray.count - 2), 0) animated:NO];
        }
        
    }
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.scroll.contentOffset.x / self.scroll.frame.size.width -1;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
